package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.GoodsTypeComboDto;
import com.dingreading.cloud.admin.entity.GoodsTypeCombo;
import com.dingreading.cloud.admin.entity.table.GoodsTypeComboTableDef;
import com.dingreading.cloud.admin.mapper.GoodsTypeComboMapper;
import com.dingreading.cloud.admin.service.GoodsTypeComboService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.GoodsTableDef.goods;
import static com.dingreading.cloud.admin.entity.table.GoodsTypeTableDef.goodsType;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-03-21
 */
@Service
public class GoodsTypeComboServiceImpl extends BaseServiceImpl<GoodsTypeComboMapper, GoodsTypeCombo, GoodsTypeComboTableDef> implements GoodsTypeComboService {

    @Override
    protected GoodsTypeComboTableDef getTable() {
        return GoodsTypeComboTableDef.goodsTypeCombo;
    }

    @Override
    public boolean haveGoodsId(Long goodsId) {
        return exists(table.goodsId.eq(goodsId));
    }

    @Override
    public boolean havesGoodsTypeIds(Long goodsTypeId) {
        return exists(table.goodsTypeId.eq(goodsTypeId));
    }

    @Override
    public List<GoodsTypeCombo> listByMainGoodsIdAndMainGoodsTypeId(Long mainGoodsId, Long mainGoodsTypeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.mainGoodsId.eq(mainGoodsId))
                .and(table.mainGoodsTypeId.eq(mainGoodsTypeId));

        return getMapper().selectListByCondition(condition);
    }


    @Override
    public List<GoodsTypeCombo> listByMainGoodsTypeIds(List<Long> mainGoodsTypeIds) {
        return getMapper().selectListByCondition(table.mainGoodsTypeId.in(mainGoodsTypeIds));
    }

    @Override
    public List<GoodsTypeComboDto> dtoListByMainGoodsTypeIds(List<Long> mainGoodsTypeIds) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, goods.name.as("goodsName"), goodsType.name.as("goodsTypeName"),
                        goodsType.weight, goodsType.warnNum, goodsType.freeDeliveryStatus)
                .from(table)
                .leftJoin(goods).on(table.goodsId.eq(goods.id))
                .leftJoin(goodsType).on(table.goodsTypeId.eq(goodsType.id))
                .where(table.mainGoodsTypeId.in(mainGoodsTypeIds))
                .orderBy(table.id.asc());
        return getMapper().selectListByQueryAs(wrapper, GoodsTypeComboDto.class);
    }

    @Override
    public void deleteByMainGoodsIdAndMainGoodsTypeId(Long goodsId, Long goodsTypeIds) {
        getMapper().deleteByCondition(table.mainGoodsId.eq(goodsId).and(table.mainGoodsTypeId.eq(goodsTypeIds)));
    }

    @Override
    public void deleteByMainGoodsId(Long goodsId) {
        getMapper().deleteByCondition(table.mainGoodsId.eq(goodsId));
    }
}
